package io.github.zhx666666.word;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

import java.io.*;
import java.util.*;

public class MergeWordDocumentsWithStyles {
    public static void main(String[] args) {
        try {
            FileInputStream targetFileStream = new FileInputStream("H:\\aa.docx");
            XWPFDocument targetDocument = new XWPFDocument(targetFileStream);
            targetFileStream.close();

            FileInputStream sourceFileStream = new FileInputStream("H:\\bb.docx");
            XWPFDocument sourceDocument = new XWPFDocument(sourceFileStream);
            sourceFileStream.close();


            String targetTitle = "ada";


            // 在这里添加逻辑以找到目标标题
            boolean foundTargetTitle = false;
            // 复制段落和样式
            for (XWPFParagraph paragraph : sourceDocument.getParagraphs()) {
                if (!foundTargetTitle) {
                    if (paragraph.getText().contains(targetTitle)) {
                        foundTargetTitle = true;
                    }
                    XWPFParagraph newParagraph = targetDocument.createParagraph();
                    newParagraph.getCTP().setPPr(paragraph.getCTP().getPPr());



                    for (XWPFRun run : paragraph.getRuns()) {
                        XWPFRun newRun = newParagraph.createRun();
                        newRun.getCTR().setRPr(run.getCTR().getRPr());
                        //复制段落
                        newRun.setText(run.text());


                        // 复制段落中的图片

                        for (XWPFPicture picture : run.getEmbeddedPictures()) {
                            XWPFPictureData pictureData = picture.getPictureData();
                            //byte[] pictureBytes = pictureData.getData();
                            //int pictureType = pictureData.getPackagePart().getContentType().getPackagePart().getPackage().getContentType();
                            int pictureType = pictureData.getPictureType();//pictureData.getPackagePart().getContentType().getPackagePart().getPackage().getContentType();
                            InputStream inputStream = picture.getPictureData().getPackagePart().getInputStream();
                            //获取文件属行
                            long width = 0;
                            long height = 0;



                            // 获取图片的尺寸信息
                            CTPositiveSize2D extent = picture.getCTPicture().getSpPr().getXfrm().getExt();

                            if (extent != null) {
                                width = extent.getCx();
                                height = extent.getCy();

                                // 如果需要将尺寸从英寸（1/360000）转换为磅（1/20）
//                                width = (long) (Units.toPoints(width) / 20);
//                                height = (long) (Units.toPoints(height) / 20);
                            }

                            // 在目标文档中创建新的图片对象
                            try {
                                XWPFPicture newPicture = newParagraph.createRun().addPicture(
                                        inputStream, pictureType, pictureData.getFileName(), Units.toEMU(Units.toPoints(width)), Units.toEMU(Units.toPoints(height))
                                );
                            } catch (InvalidFormatException e) {
                                throw new RuntimeException(e);
                            }
                        }

                    }
                }
            }


            // 保存合并后的文档
            FileOutputStream mergedFileStream = new FileOutputStream("H:\\cc1qq222cc.docx");
            targetDocument.write(mergedFileStream);
            mergedFileStream.close();

            System.out.println("文档合并完成。");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

